<?php
class ListFolder{
    function listDir($path, $filter = NULL, $level = 0, $start = 1, $test = 0, &$count = 0) {
        if ($level) {
            $count_path = count(explode("/", $path));
            if ($count_path == $level) {
                return NULL;
            }
        }
        if ($test) {
            echo "path : ";
            var_dump($path);
            echo " - count : ";
            var_dump(count(explode("/", $path)));
            echo " - level : ";
            var_dump($level);
        }
        // if($start) var_dump($path);
        global $root_path;
        $array = array();
        $filter_false = 0;
        $count++;

        //using the opendir function
        $dir_handle = @opendir($path) or die("Unable to open $path at step = $count");

        //Leave only the lastest folder name
        $dirname = end(explode("/", $path));
        //display the target folder.
        // if($start) var_dump($dirname);
        if ($filter && $dirname) {
            if (!stristr($filter, $dirname)) {
                $array[0]->dir = $dirname;
            } else {
                $filter_false = 1;
            }
        } else {
            $array[0]->dir = $dirname;
        }
        if ($test) {
            echo " - array dir : ";
            var_dump($array[0]->dir);
        }

        while (false !== ($file = readdir($dir_handle)) && !$filter_false) {
            $num = count($array) == 0 ? 0 : count($array);
            if ($test) {
                echo " - while path : ";
                var_dump($path);
                echo " - file : ";
                var_dump($file);
                echo " - is dir : ";
                var_dump(is_dir($path . "/" . $file));
            }
            if ($file != "." && $file != "..") {
                if (is_dir($path . "/" . $file)) {
                    //Display a list of sub folders.
                    if ($level) {
                        $count_path = count(explode("/", $path));
                        if ($test) {
                            echo " - count path : ";
                            var_dump($count_path);
                            echo " - count path = count level : ";
                            var_dump($count_path == $level);
                            echo " - count level : ";
                            var_dump($level);
                            echo "___________________<br />";
                        }
                        if ($count_path == $level) {
                            return NULL;
                        }
                    }
                    if ($filter) {
                        $dirname = end(explode("/", $path));
                        if (isset($dirname) && $dirname) {
                            if (stristr($filter, $dirname)) {
                                return NULL;
                                //                        continue;
                            }
                        }
                    }
                    $tmp = $this->listDir($path . "/" . $file, $filter, $level, 0, $test, $count);
                    if ($tmp)
                        $array[$num]->sub = $tmp;
                }
                else {
                    if ($level) {
                        $count_path = count(explode("/", $path));
                        if ($count_path == $level) {
                            return NULL;
                        }
                    }
                    if ($filter) {
                        if (stristr($filter, $file)) {
                            return NULL;
                        }
                    }
                    //Display a list of files.
                    $array[$num]->files = $file;
                }
            }
        }

        //closing the directory
        closedir($dir_handle);
        return $array;
    }
    
    function _list_array($var = false){
        if($var){
            $this->_list_array = $var;
        }else{
            return $this->_list_array;
        }
    }

    function display_tree($array, $start = 1) {
        $html = "";

        if ($start)
            $array[0]->dir = "&nbsp;";
        $go = "<a href='/{$array[0]->dir}' class='click-over' style='text-align:right; float:right;'>&nbsp;&nbsp;&nbsp;Go</a>";
        $html = "<li>$go<a href='#' style='' class='toggle'>" . $array[0]->dir . "</a>";
        $html .= "<ul>";
        foreach ($array as $key => $value) {
            if ($key) {
                if (isset($value->files)) {
                    $html .= "<li><a href='#'>" . $value->files . "</a>";
                } else if (isset($value->sub)) {
                    if (isset($value->sub->dir) && $value->sub->dir != "recycle_bin") {
                        $html .= "</li></ul>";
                        break;
                    }
                    $html .= $this->display_tree($value->sub, 0);
                    if (isset($array[0]->files))
                        $html .= "</li></ul>";
                    else
                        $html .= "</ul>";
                }
            }
        }

        // Close 
        if ($start)
            $html .= "</ul>";
        return $html;
    }

    /*****************************************************************************************\
    //---------------------------------- For Download File ----------------------------------\\
    \*****************************************************************************************/

    function display_tree_download($array, $start = 1, $last_dir = "") {
        global $base_url;
        $dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : "compliance";
        $menu = isset($_REQUEST['menu']) ? $_REQUEST['menu'] : "1";
        $html = "";

        if ($start)
            $array[0]->dir = "&nbsp;";
        $html = "<li><a href='#'>" . $array[0]->dir . "</a>";
        $html .= "<ul>";
        $current_dir = "";
        foreach ($array as $key => $value) {
            if (isset($value->dir) && !empty($value->dir) && $value->dir != "&nbsp;") {
                $last_dir = trim(isset($last_dir) && $last_dir != "/" ? trim($last_dir) : "");
                if (isset($last_dir) && !empty($last_dir)) {
                    $current_dir = $last_dir;
                }
                $current_dir .= $value->dir . "/";
            }
            if ($key) {
                if (isset($value->files)) {
                    $html .= "<li><a href='$base_url/manager/$dir:$menu:" . $current_dir . $value->files . ".html'>" . $value->files . "</a>";
                } else if (isset($value->sub)) {
                    if (isset($value->sub->dir) && $value->sub->dir != "recycle_bin") {
                        $html .= "</li></ul>";
                        break;
                    }
                    $html .= display_tree($value->sub, 0, $current_dir);
                    if (isset($array[0]->files))
                        $html .= "</li></ul>";
                    else
                        $html .= "</ul>";
                }
            }
        }

        // Close 
        if ($start)
            $html .= "</ul>";
        return $html;
    }

    function downloadfile($path, $v) {
        //BUILD THE FILE INFORMATION
        $file = $path . $v;

        //CREATE/OUTPUT THE HEADER
        header("Content-type: application/force-download");
        header("Content-Transfer-Encoding: Binary");
        header("Content-length: " . filesize($file));
        header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
        readfile($file);
    }

    /*****************************************************************************************\
    \\---------------------------------- For Download File ----------------------------------//
    \*****************************************************************************************/
}
?>